<?php 
include('./fun.php');include('./hd_fun.php');
$conn=my_sql($cfg);
$re=user_login();
if($re['err']=='no_login'){direct('login.php?t='.time());exit;}
if($re['err']!='ok'){die($re['err']);}
$user=$re['user'];
$shop_id=$re['shop_id'];
$shop=get_shop();
$set=get_set();

$sql='select * from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" ';
$c=g('c');
if($c){$sql.=' and c=:c ';$p['c']=$c;}
$state=g('state');
if($state){
if($state=='stop'){$ss=0;}else{$ss=1;}
$sql.=' and state="'.$ss.'" ';
}

$key=g('key');
if($key){$sql.=' and temp like "%'.a($key).'%" ';}

$page=g('page');if(!$page){$page=1;}
$page_num=10;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);

if($l){
$sql='select c from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" ';
$cc=db($sql);
if($cc){
foreach($cc as $v){
    if($v['c']){
$cx[$v['c']]=$v['c'];
}
}
}
}
?>
<style>
pro_list img{width:80px;}
iss{float:left;width:100%;margin-top:5px;}
iss a{padding:5px;color:#fff;background:#ccc;border-radius:5px;font-size:11px}
iss .select{background:crimson;}
iss a:hover{color:#fff}
pro_info{float:left;width:100%;height:20px;line-height:20px;overflow:hidden;color:#999;margin-top:5px}
index tx{float:left;height:40px;line-height:40px;color:#999;margin-right:10px;font-size:15px;}
</style>


<hh><b>商品管理</b></hh>
<mm>
<index><a class="m b" onclick="right({title:'快速创建商品',url:'select_img.php?max=9&action=quick_creat($(this))',width:'75vw'})">创建商品</a>
<?php 
$url=url_get(['page','state']);
if($state){
echo '<a href="?'.$url.'">全部</a>';
}
echo '<a class="'.($state==1?'select':'').'" href="?'.$url.'&state=1">已上架</a>';
echo '<a class="'.($state=='stop'?'select':'').'" href="?'.$url.'&state=stop">未上架</a>';
?>
<input placeholder="请输入关键词" style="box-shadow:none" id="key" value="<?php echo $key;?>"><a onclick="search_it($(this))">搜索</a>
</index>

<?php 
if($cx){
echo '<index style="margin-top:5px"><tx>分类:</tx>';
$url=url_get(['page','c','key']);
foreach($cx as $v){
echo '<a class="m '.($c==$v?'select':'').'" href="?'.$url.'&c='.$v.'">'.$v.'</a>';
}
echo '</index>';
}

?>


<pro_list>
<?php 
if($l){
echo '<table>';
th(['图片','名称/参数','价格','活动商品','操作']);
foreach($l as $v){
$img=get_img($v['img'],'_240');

echo '<tr>';
echo '<td style="width:100px"><img src="'.$img.'" onclick="select_img($(this))" i="'.$v['id'].'" img="'.$v['img'].'"></td>';
echo '<td>
<pro_title>'.base64($v['title']).'</pro_title><br>
<c>'.($v['c']?'分类:'.$v['c']:'未分类').'</c>
<iss><a class="'.($v['is_new']?'select':'').'" onclick="change_val($(this))" i="'.$v['id'].'" c="is_new" x="a" val="'.($v['is_new']?'stop':'1').'">新</a><a class="'.($v['is_hot']?'select':'').'" onclick="change_val($(this))" i="'.$v['id'].'" c="is_hot" x="a" val="'.($v['is_hot']?'stop':'1').'">热</a></iss>
<pro_info>'.$v['info'].'</pro_info>
</td>';

echo '<td>
<price style="color:crimson;">￥'.$v['price'].'</price><br>
<old style="color:#999;text-decoration:line-through;">￥'.$v['old'].'</old><br>
'.($v['total']==-1?'无限库存':($v['total']>0?$v['total']:'缺货')).'

</td>';

echo '<td>
<a onclick="change_val($(this))" i="'.$v['id'].'" c="is_hide" x="a" val="'.($v['is_hide']?'stop':'1').'">'.($v['is_hide']?'是':'否').'</a>
</td>';

echo '<td>
'.($v['state']==1?'<a onclick="state($(this))" val="stop" i="'.$v['id'].'">停用</a>':'<a onclick="del($(this))"  i="'.$v['id'].'">删除</a><a onclick="state($(this))" val="1" i="'.$v['id'].'">上架</a>').'
<a onclick="edit($(this))" i="'.$v['id'].'">修改</a>
</td>';
echo '</tr>';
}
echo '</table>';

page();
}else{no_data();}


?>


</pro_list>

</mm>



<script>
// function quick_creat(o){
// var img=o.attr('x');
// var data={img:img}
// post('quick_creat_pro',data,'',function(res){
//     msg('商品创建成功');
//     main_load();
// });
// }
function quick_creat(o){
//var img=o.attr('x');
    var im='';
    if(window.is_select_img){
        for(var i in window.is_select_img){
            im+=$('.img_item_'+window.is_select_img[i]).attr('x')+'||';
        }
    }

    var data={img:im}
    post('quick_creat_pro',data,'',function(res){
        msg('商品创建成功');
        main_load();
    });
}

function state(o){
var id=o.attr('i');if(!id){msg('请指定商品');return;}
var val=o.attr('val');if(!val){val='stop';}
post('pro_change',{state:val},id,function(res){
    main_load();
    
});

}

function del(o){
var id=o.attr('i');if(!id){msg('请指定商品');return;}
if(!confirm("是否删除商品，删除商家将会影响所有使用该商品的活动，是否继续?")){return;}
post('pro_del','',id,function(res){
    main_load();
    msg('删除成功');
})
}

function change_val(o){
var id=o.attr('i');if(!id){msg('请指定商品');return;}

var c=o.attr('c');
var x=o.attr('x');
if(x=='a'){
var val=o.attr('val');
}else{
var val=o.val();
}


if(val && c){
post('pro_change',{[c]:val},id,function(res){
main_load();
});
}
}


function edit(o){
var id=o.attr('i');
right({title:'编辑商品信息',url:'pro_edit.php?id='+id,width:'700px'})
}


function search_it(o){
var url='<?php echo url_get(['page','key','state']);?>';
var key=$('#key').val();if(!key){key='';}
location.href='?'+url+'&key='+key;
}


function select_img(o){
var img=o.attr('img');
window.img_obj=o;
right({title:'选择商品图片',url:'select_img.php?action=change_pro_img($(this))&max=5&img='+img,width:'75vw'})
}

function change_pro_img(o){
if(!window.is_select_img){window.is_select_img={}}
var num=Object.keys(window.is_select_img).length;
if(num==0){msg('请选择图片');return;}
var img='';
for(var i in window.is_select_img){
var obj=$('.'+i);
var im=obj.attr('x');
img+=im+'||'
}

var data={img:img}
post('pro_change',data,window.img_obj.attr('i'),function(res){
    msg('修改成功');
    main_load();
    right_close();
});

}

</script>